package io.kiki.sba.registry.server.metadata.bootstrap.config;

import io.kiki.sba.registry.common.model.metaserver.Lease;
import io.kiki.sba.registry.server.shared.config.CommonConfig;
import io.kiki.sba.registry.util.OsUtils;
import io.kiki.sba.registry.util.SystemUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Setter
@Getter
@ConfigurationProperties(prefix = ServerConfigBean.PREFIX)
public class ServerConfigBean implements ServerConfig {

    public static final String PREFIX = "meta.server";
    private final CommonConfig commonConfig;
    private int sessionServerPort = 9610;
    private int dataServerPort = 9611;
    private int metaServerPort = 9612;
    private int httpServerPort = 9615;
    private int schedulerHeartbeatIntervalSecs = 1;
    private int dataNodeExchangeTimeoutMillis = 3000;
    private int sessionNodeExchangeTimeoutMillis = 3000;
    private int metaNodeExchangeTimeoutMillis = 3000;
    private int defaultRequestExecutorMinSize = OsUtils.getCpuCount() * 5;
    private int defaultRequestExecutorMaxSize = OsUtils.getCpuCount() * 10;
    private int defaultRequestExecutorQueueSize = 500;
    private int expireCheckIntervalMillis = 1000;
    private int revisionGcSilenceHour = 24;
    private int revisionGcInitialDelaySecs = 60;
    private int revisionGcSecs = 60;
    private int clientManagerWatchMillis = 1000;
    private int clientManagerRefreshMillis = 300 * 1000;
    private int clientManagerRefreshLimit = 5000;
    private int clientManagerCleanSecs = 300;
    private int clientManagerExpireDays = 1;
    private int appRevisionMaxRemove = 2000;
    private int interfaceMaxRemove = 2000;
    private int appRevisionCountAlarmThreshold = 20;
    private long metaLeaderWarmupMillis = SystemUtils.getSystemLong("registry.elector.warm.up.millis", TimeUnit.SECONDS.toMillis(Lease.DEFAULT_DURATION_SECS * 3 / 2));
    private long dataReplicateMaxGapMillis = SystemUtils.getSystemLong("registry.data.replicate.max.gap.millis", 3 * 60 * 1000);

    // <=0 means no protection
    private int metaSchedulerPoolSize = OsUtils.getCpuCount();
    private int dataNodeProtectionNum = SystemUtils.getSystemInteger("registry.data.protection.num", 0);

    public ServerConfigBean(CommonConfig commonConfig) {
        this.commonConfig = commonConfig;
    }

    @Override
    public String getLocalDataCenter() {
        return commonConfig.getDataCenter();
    }

    @Override
    public boolean isLocalDataCenter(String dataCenter) {
        return commonConfig.getDataCenter().equals(dataCenter);
    }

    @Override
    public Set<String> getLocalDataCenterZones() {
        return commonConfig.getLocalSegmentRegions();
    }

}
